package vrp;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import vrp.Vehicle;
import vrp.PMF;

public class VehicleAddServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {

    	long parterID = Integer.parseInt(req.getParameter("partner"));
        Key partnerKey = KeyFactory.createKey(Partner.class.getSimpleName(), parterID);
        String name = req.getParameter("name");
        double capacity = Double.parseDouble(req.getParameter("capacity"));
        double operatingCost = Double.parseDouble(req.getParameter("operatingCost"));
        int vehicleStatus = Integer.parseInt(req.getParameter("vehicleStatus"));

        Vehicle vehicle = new Vehicle(partnerKey, name, capacity, operatingCost, vehicleStatus);
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(vehicle);
        } finally {
            pm.close();
        }
        resp.sendRedirect("/vehicles.jsp");
    }
}